package com.six.epidemic.report.controller

import com.github.pagehelper.PageHelper
import com.github.pagehelper.PageInfo
import com.six.library.annotation.JWTIgnore
import com.six.epidemic.report.pojo.EpidemicReport
import com.six.epidemic.report.pojo.vo.SearchEpidemicReport
import com.six.epidemic.report.service.IEpidemicReportService
import com.six.library.model.ResultData
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource

@RefreshScope
@RestController
@RequestMapping("/epireport")
@CrossOrigin
class EpidemicReportController {
    @Resource
    private lateinit var epidemicReportService: IEpidemicReportService
    @JWTIgnore
    @RequestMapping("/add")
    fun add(@RequestBody model: EpidemicReport?): ResultData {
        val result = ResultData()
        try {
            if (model == null) {
                return result.setMsg("上报疫情数据出错,请与管理员联系").setCode(300)
            }
            val rows = epidemicReportService.add(model)
            if (rows > 0) {
                result.setMsg("success").statusCode = 200
            } else {
                result.statusCode = 300
                if (rows == -10) {
                    result.statusCode = 310
                    result.setMsg("对不起，上报同一个人数据，在10分钟内只能提交一次，请10分钟后再试。")
                } else result.setMsg("上报疫情数据出错,请与管理员联系")
            }
        } catch (ex: Exception) {
            result.statusCode = 500
            result.setMsg("fail")
            println("上报疫情数据出错,请与管理员联系")
        }
        return result
    }
    @JWTIgnore
    @RequestMapping(*["/getpagelist/{pageIndex}", "/getpagelist", "/getpagelist/{pageIndex}/{pageSize}"])
    fun getPageList(
        @PathVariable(value = "pageIndex", required = false) pageIndex: Int?,
        @PathVariable(value = "pageSize", required = false) pageSize: Int?,
        @RequestBody model: SearchEpidemicReport?
    ): ResultData {
        var pageIndex = pageIndex
        var pageSize = pageSize
        val result = ResultData()
        try {
            //判断pageIndex是否是有效数据
            pageIndex = if (pageIndex == null || pageIndex < 0) 1 else pageIndex
            //判断pageSize是否是有效数据
            pageSize = if (pageSize == null || pageSize < 1) 15 else pageSize
            // 分页查询数据
            PageHelper.startPage<EpidemicReport>(pageIndex, pageSize)
            val list = if (model != null) {
                epidemicReportService.getPageListByQuery(model)
            } else {
                epidemicReportService.findAll()
            }

            val pageInfo = PageInfo(list)
            if (pageInfo != null && pageInfo.total > 0) {

                result.setCode(200).setMsg("success").setData(pageInfo)
            } else {
                result.setCode(404).setMsg("没有查询到数据")
            }
        } catch (ex: Exception) {

            result.setCode(500).setMsg("查询数据出错，请与管理员了联系")
        }
        return result
    }

    @JWTIgnore
    @RequestMapping("/geeet")
    fun getPageListByQuery(): ResultData {
        val result = ResultData()
        val pageListByQuery = epidemicReportService.getPageListByQuery(SearchEpidemicReport().apply {
            this.startTime = "2022-10-18"
            this.endTime = "2022-10-30"
        })

        return result.setData(pageListByQuery)
    }

}
